package com.hw.one.collect.ionsoup.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hw.one.collect.ionsoup.constants.IonSoupType;
import com.hw.one.collect.ionsoup.entity.AirCycle;
import com.hw.one.collect.ionsoup.entity.Results;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/2/28.
 */
public class AirConcUtil extends DefaultHandler {
	private Results<AirCycle> results;
	private List<AirCycle> cycleList;
	private AirCycle cycle;

//    public static final String xmlPath = "C:\\Users\\guo\\Desktop\\雪城协议\\数据传输使用\\离子色谱仪\\七天临时数据\\AirConc.xml";
	public Results<AirCycle> parse(String filePath) {
		try {
			XMLReader xr = XMLReaderFactory.createXMLReader();
			xr.setContentHandler(this);
			xr.parse(new InputSource(new FileReader(filePath)));
			return this.results;
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void startDocument() throws SAXException {
		if (null == results) {
			results = new Results<AirCycle>(IonSoupType.AIR);
		}
		if (null == cycleList) {
			cycleList = new ArrayList<>();
		}
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if ("cycle".equals(qName)) {
			cycle = new AirCycle();
			if (null != attributes && attributes.getLength() > 0) {
				for (int i = 0; i < attributes.getLength(); i++) {
					String attrQname = attributes.getQName(i);
					String attrValue = attributes.getValue(i);
					if (null == attrQname) {
						continue;
					}
					switch (attrQname) {
					case "id":
						cycle.setId(attrValue);
						break;
					case "samplebox":
						cycle.setSamplebox(attrValue);
						break;
					case "statusword":
						cycle.setStatusword(attrValue);
						break;
					case "start":
						cycle.setStart(attrValue);
						break;
					case "end":
						cycle.setEnd(attrValue);
						break;
					case "Ca":
						cycle.setCa(attrValue);
						break;
					case "Ca-quality":
						cycle.setCaQuality(attrValue);
						break;
					case "Cl":
						cycle.setCl(attrValue);
						break;
					case "Cl-quality":
						cycle.setClQuality(attrValue);
						break;
					case "HCl":
						cycle.setHCl(attrValue);
						break;
					case "HCl-quality":
						cycle.setHClQuality(attrValue);
						break;
					case "HNO2":
						cycle.setHNO2(attrValue);
						break;
					case "HNO2-quality":
						cycle.setHNO2Quality(attrValue);
						break;
					case "HNO3":
						cycle.setHNO3(attrValue);
						break;
					case "HNO3-quality":
						cycle.setHNO3Quality(attrValue);
						break;
					case "K":
						cycle.setK(attrValue);
						break;
					case "K-quality":
						cycle.setKQuality(attrValue);
						break;
					case "Mg":
						cycle.setMg(attrValue);
						break;
					case "Mg-quality":
						cycle.setMgQuality(attrValue);
						break;
					case "NH3":
						cycle.setNH3(attrValue);
						break;
					case "NH3-quality":
						cycle.setNH3Quality(attrValue);
						break;
					case "NH4":
						cycle.setNH4(attrValue);
						break;
					case "NH4-quality":
						cycle.setNH4Quality(attrValue);
						break;
					case "NO3":
						cycle.setNO3(attrValue);
						break;
					case "NO3-quality":
						cycle.setNO3Quality(attrValue);
						break;
					case "Na":
						cycle.setNa(attrValue);
						break;
					case "Na-quality":
						cycle.setNaQuality(attrValue);
						break;
					case "SO2":
						cycle.setSO2(attrValue);
						break;
					case "SO2-quality":
						cycle.setSO2Quality(attrValue);
						break;
					case "SO4":
						cycle.setSO4(attrValue);
						break;
					case "SO4-quality":
						cycle.setSO4Quality(attrValue);
						break;
					default:
						break;
					}
				}
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
//        System.out.println("enter end element");
		if ("cycle".equals(qName)) {
			cycleList.add(cycle);
		}
		if ("results".equals(qName)) {
			results.setLiquidCycleList(cycleList);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
//        System.out.println(tempStr + "---------------------->characters" + "  " + preTag);
//        if(null != person){
//            String tempStr = new String(ch, start, length);
//            if("name".equals(preTag)){
//                person.setName(tempStr);
//            }else if("age".equals(preTag)){
//                person.setAge(Integer.valueOf(tempStr));
//            }else if("idCard".equals(preTag)){
//                person.setIdCard(tempStr);
//            }
//        }
	}

}
